package com.shanghaiyimu.yimiyangche.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.UserAdapter;
import com.shanghaiyimu.yimiyangche.bean.User;
import com.shanghaiyimu.yimiyangche.myclass.ChineseToEnglish;
import com.shanghaiyimu.yimiyangche.myclass.CompareSort;
import com.shanghaiyimu.yimiyangche.widget.SideBarView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CarModeChooseActivity extends AppCompatActivity implements SideBarView.LetterSelectListener,AdapterView.OnItemClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_mode_choose);
        initView();
    }

    ListView mListview,choose_car_mode,choose_car_pailiang;
    UserAdapter mAdapter;
    TextView mTip;
    String[] contactsArray;
    private DrawerLayout drawerLayout,linearLayout;

    private void initView() {
        choose_car_pailiang = (ListView) findViewById(R.id.choose_car_pailiang);
        choose_car_pailiang.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData1()));
        choose_car_pailiang.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("mode","奥迪A8L 2017款 2.0L 手动");
                CarModeChooseActivity.this.setResult(0,intent);
                CarModeChooseActivity.this.finish();
            }
        });
        choose_car_mode = (ListView) findViewById(R.id.choose_car_mode);
        choose_car_mode.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
        choose_car_mode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                linearLayout.openDrawer(choose_car_pailiang);
            }
        });


        linearLayout = (DrawerLayout) findViewById(R.id.linerlayout_choose);
        drawerLayout = (DrawerLayout) findViewById(R.id.v4_drawerlayout);
        mListview = (ListView) findViewById(R.id.listview);
        SideBarView sideBarView = (SideBarView) findViewById(R.id.sidebarview);
        contactsArray = getResources().getStringArray(R.array.data);
//        String[] headArray = getResources().getStringArray(R.array.head);
        mTip = (TextView) findViewById(R.id.tip);

        //模拟添加数据到Arraylist
        int length = contactsArray.length;
        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            User user = new User();
            user.setName(contactsArray[i]);
            String firstSpell = ChineseToEnglish.getFirstSpell(contactsArray[i]);
            String substring = firstSpell.substring(0, 1).toUpperCase();
            if(substring.matches("[A-Z]")){
                user.setLetter(substring);
            }else {
                user.setLetter("#");
            }
            users.add(user);
        }

//        for (int i = 0; i < headArray.length; i++) {
//            User user = new User();
//            user.setName(headArray[i]);
//            user.setLetter("@");
//            users.add(user);
//        }

        //排序
        Collections.sort(users, new CompareSort());

        //设置数据
        mAdapter = new UserAdapter(this);
        mAdapter.setData(users);
        mListview.setAdapter(mAdapter);

        //设置回调
        sideBarView.setOnLetterSelectListen(this);
        mListview.setOnItemClickListener(this);



    }

    private List<String> getData(){

        List<String> data = new ArrayList<String>();
        data.add("A1");
        data.add("A2");
        data.add("A3");
        data.add("A4");
        data.add("A5");
        data.add("A6");
        data.add("A7");
        data.add("A8");
        data.add("A8L");

        return data;
    }
    private List<String> getData1(){

        List<String> data = new ArrayList<String>();
        data.add("2015款1.4L手动");
        data.add("2015款1.5L手动");
        data.add("2015款1.6L手动");
        data.add("2016款1.8L手动");
        data.add("2016款1.9L手动");
        data.add("2016款2.0L手动");
        data.add("2017款2.0L手动");
        data.add("2017款2.4L手动");
        data.add("2017款2.2L手动");

        return data;
    }

    @Override
    public void onLetterSelected(String letter) {
        setListviewPosition(letter);
        mTip.setText(letter);
        mTip.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLetterChanged(String letter) {
        setListviewPosition(letter);
        mTip.setText(letter);
    }

    @Override
    public void onLetterReleased(String letter) {
        mTip.setVisibility(View.GONE);
    }

    private void setListviewPosition(String letter){
        int firstLetterPosition = mAdapter.getFirstLetterPosition(letter);
        if(firstLetterPosition != -1){
            mListview.setSelection(firstLetterPosition);
        }
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        User user = (User) parent.getAdapter().getItem(position);

        drawerLayout.openDrawer(linearLayout);

//        Intent intent = new Intent();
//        intent.putExtra("mode",user.getName());
//        this.setResult(0,intent);
//        this.finish();
    }

    String BACK_CODE ="uuu";
    int BACK_CODE_NO = 99;
    @Override
    public void onBackPressed() {
        //数据是使用Intent返回
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra(BACK_CODE, BACK_CODE_NO);
        //设置返回数据
        this.setResult(RESULT_OK, intent);
        //关闭Activity
        this.finish();
    }
    private void showDrawerLayout(){
        if (!drawerLayout.isDrawerOpen(Gravity.RIGHT)){
            drawerLayout.openDrawer(Gravity.RIGHT);
        }else {
            drawerLayout.closeDrawer(Gravity.RIGHT);
        }
    }
}
